<?php

namespace App\Models;

use App\App\Services\Web\UserService;
use Illuminate\Container\Container;

trait Groupable
{
    protected static function boot()
    {
        parent::boot();

        /**
         * Attach to the 'creating' Model Event to provide a UUID
         * for the `id` field (provided by $model->getKeyName())
         */
        static::creating(function ($model) {
            $model->setAttribute('group_code', static::getGroupCode());
        });
    }

    public function scopeGrouped()
    {
        return $this->where('group_code', static::getGroupCode());
    }

    public static function getGroupCode()
    {
        return Container::getInstance()->make(UserService::class)->getGroupCode();
    }
}
